/**
 * Copyright (c) 2021-2030, NDWP.
 * <p>
 */
package com.example.waternetwork.api.utils;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * Mybatis-plus代码生成
 *
 * @ Author     ：Chenyong.
 * @ Date       ：Created in 11:04 2025/6/16
 */
public class CodeGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:postgresql://192.168.102.27:54325/water-network",
                        "wn", "3=rsTT3S")
                .globalConfig(builder -> {
                    builder.author("auto gen") // 设置作者
                            .outputDir("D://autogen"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        }).schema("supervision")
                )
                .packageConfig(builder ->
                        builder.parent("com.example.waternetwork.api") // 设置父包名
                                .moduleName("supervision") // 设置父包模块名
                )
                .strategyConfig(builder ->
                        builder.addInclude("data_reservoir_rainfall,data_water_level")// 设置需要生成的表名
                                .entityBuilder()
                                .enableLombok() // 启用 Lombok
                                .enableTableFieldAnnotation() // 启用字段注解
                                .controllerBuilder()
                                .enableRestStyle()
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }
}